package com.example.ymm.modules1.service.impl.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.mapper.my.user.DbBookMapper;
import com.example.ymm.modules1.service.dd.DingFileService;
import com.example.ymm.modules1.service.dto.DbBookQueryCriteria;
import com.example.ymm.modules1.service.dto.DbClazzBookQueryCriteria;
import com.example.ymm.modules1.service.dto.DbClazzLevelQueryCriteria;
import com.example.ymm.modules1.service.user.DbBookService;
import com.example.ymm.modules1.service.user.DbClazzBookService;
import com.example.ymm.modules1.service.user.DbClazzLevelService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DbBookServiceImpl extends BaseServiceImpl<DbBookMapper, DbBook> implements DbBookService {

    @Autowired
    DbUserService dbUserService;
    @Autowired
    DbClazzLevelService dbClazzLevelService;
    @Autowired
    DbClazzBookService dbClazzBookService;

    @Autowired
    DingFileService dingFileService;

    @Override
    public TableList<DbBook> list(DbBookQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbBook> page = new Page<DbBook>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbBook.class, criteria);
        List<String> list=new ArrayList<>();
        list.add("num");
        list.add("create_time");

        predicate.orderByDesc(list);
        baseMapper.selectPage(page, predicate);
        TableList<DbBook> tableList=new TableList(page);
        return tableList;
    }

    public List<DbBook> queryAll(DbBookQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbBook.class, criteria);
        List<String> list=new ArrayList<>();
        list.add("num");
        list.add("create_time");
        predicate.orderByDesc(list);
        List<DbBook> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<ViewBook> getUserBook(DbClazzBookQueryCriteria criteria) {
        String userId = getUserId();
        DbUser user = dbUserService.getById(userId);
        List<ViewBook> books=null;
        if(UtilValidate.isNotEmpty(user.getLevelName())){
            DbClazzLevelQueryCriteria clazzLevelQuery=new DbClazzLevelQueryCriteria();
            clazzLevelQuery.setLevelName(user.getLevelName());

            List<DbClazzLevel> list = dbClazzLevelService.queryAll(clazzLevelQuery);
            if(UtilValidate.isNotEmpty(list)){
                DbClazzLevel dbClazzLevel = list.get(0);
                criteria.setClazzId(dbClazzLevel.getClazzId());
                books = dbClazzBookService.queryAll(criteria);
            }
        }
        return books;
    }


    @Override
    public DbBook detail(String bookId) {
        DbBook book = getById(bookId);
        if(UtilValidate.isNotEmpty(book)&&UtilValidate.isNotEmpty(book.getFileInfo())){
            JSONObject jsonObject = JSONObject.parseObject(book.getFileInfo());
            dingFileService.updateFileUserPermissions(jsonObject,bookId);
        }
        return book;
    }


    @Override
    public DbBook readBook(String bookId) {
        DbBook book = getById(bookId);
        if(UtilValidate.isNotEmpty(book)&&UtilValidate.isNotEmpty(book.getFileInfo())){
            JSONObject jsonObject = JSONObject.parseObject(book.getFileInfo());
            dingFileService.updateFileUserPermissions(jsonObject,bookId);
        }
        return book;
    }
}
